const dynamicRoutes = [
  {
    name: "systemManage",
    path: "/systemManage",
    hidden: false,
    redirect: "noRedirect",
    component: "Layout",
    alwaysShow: true,
    meta: {
      title: "系统管理",
      icon: "icon-xitongguanli",
      noCache: false,
      link: null,
    },
    children: [
      {
        name: "announcementManagement",
        path: "announcementManagement",
        hidden: false,
        component: "SystemManage/announcementManagement",
        meta: {
          title: "公告管理",
          icon: "user",
          noCache: false,
          link: null,
        },
      },
      {
        name: "roleManagement",
        path: "roleManagement",
        hidden: false,
        component: "SystemManage/roleManagement",
        meta: {
          title: "角色管理",
          icon: "peoples",
          noCache: false,
          link: null,
        },
      },
    ],
  },
  {
    name: "goodsManage",
    path: "/goodsManage",
    hidden: false,
    redirect: "noRedirect",
    component: "Layout",
    alwaysShow: true,
    meta: {
      title: "产品管理",
      icon: "icon-chanpinguanli",
      noCache: false,
      link: null,
    },
    children: [
      {
        name: "goodsList",
        path: "goodsList",
        hidden: false,
        component: "GoodsManage/GoodsList/index",
        meta: {
          title: "产品列表",
          icon: "online",
          noCache: false,
          link: null,
        },
      },
      {
        name: "goodsCategory",
        path: "goodsCategory",
        hidden: false,
        component: "GoodsManage/GoodsCategory/index",
        meta: {
          title: "产品分类",
          icon: "job",
          noCache: false,
          link: null,
        },
      },
    ],
  },
  {
    name: "orderManage",
    path: "/orderManage",
    hidden: false,
    redirect: "noRedirect",
    component: "Layout",
    alwaysShow: true,
    meta: {
      title: "订单管理",
      icon: "icon-dingdanguanli35",
      noCache: false,
      link: null,
    },
    children: [
      {
        name: "OrderList",
        path: "OrderList",
        hidden: false,
        component: "OrderManage/OrderList/index",
        meta: {
          title: "订单列表",
          icon: "build",
          noCache: false,
          link: null,
        },
      },
      {
        name: "orderAggregation",
        path: "orderAggregation",
        hidden: false,
        component: "OrderManage/OrderAggregation/index",
        meta: {
          title: "订单汇总",
          icon: "code",
          noCache: false,
          link: null,
        },
      },
      {
        name: "orderReview",
        path: "orderReview",
        hidden: false,
        component: "OrderManage/OrderReview/index",
        meta: {
          title: "订单审核",
          icon: "swagger",
          noCache: false,
          link: null,
        },
      },
    ],
  },
  {
    name: "advertisementManage",
    path: "/advertisementManage",
    hidden: false,
    redirect: "noRedirect",
    component: "Layout",
    alwaysShow: true,
    meta: {
      title: "广告管理",
      icon: "icon-guanggaoguanli",
      noCache: false,
      link: null,
    },
    children: [
      {
        name: "advertisementList",
        path: "advertisementList",
        hidden: false,
        component: "AdvertisementManage/AdvertisementList/index",
        meta: {
          title: "广告列表",
          icon: "online",
          noCache: false,
          link: null,
        },
      },
    ],
  },
];
function filterDynamicRoutes(dynamicRoutes) {
  return dynamicRoutes.filter((item) => {
    if (item.component) {
      if (item.component === "Layout") {
        item.component = Layout;
      } else {
        item.component = loadView(item.component);
      }
    }
    if (item.children && item.children.length) {
      item.children = filterDynamicRoutes(item.children);
    }
    return true;
  });
}
function loadView(view) {
  const viewComponents = require.context("@/views", true, /\.vue$/, "lazy");
  const keys = viewComponents.keys();
  const match = keys.find(
    (key) => key.includes(`/${view}.vue`) || key.includes(`/${view}/index.vue`)
  );
  if (match) {
    return () => viewComponents(match);
  }
}
import Layout from "@/views/Layout";
import router from "@/router";
export default {
  namespaced: true,
  state: {
    sliderRoutes: [],
  },
  mutations: {
    SET_SLIDER_ROUTES(state, routes) {
      state.sliderRoutes = routes;
    },
  },
  actions: {
    generateRoutes(context) {
      // TODO 到时会改成接口获取数据 现在暂时使用上方假数据代替
      const sliderRoutes = filterDynamicRoutes(dynamicRoutes);
      sliderRoutes.forEach((route) => {
        router.addRoute(route);
      });
      context.commit("SET_SLIDER_ROUTES", sliderRoutes);
    },
  },
};
